<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>运动营养方案增加或修改页面</title>
    <link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
    <script type="text/javascript" src="../../res/js/Tools.js"></script>
    <script src="../../res/js/jquery-3.4.1.min.js" type="text/javascript"
            charset="utf-8"></script>
    <script src="../../js/login_common.js" charset="utf-8"></script>
    <script src="../../res/layui/layui.js" charset="utf-8"></script>
    <style>
        #btn:hover{
            background: #33aba0;
            cursor: pointer;
        }
        #btn{
            background: #009688;
            color: white;
            width: 100px;
            height: 36px;
            border: 0;
        }
        #btno:hover{
            background: #33aba0;
            cursor: pointer;
        }
        #btno{
            background: #009688;
            color: white;
            width: 100px;
            height: 36px;
            border: 0;
        }
        #btnt:hover{
            background: #33aba0;
            cursor: pointer;
        }
        #btnt{
            background: #009688;
            color: white;
            width: 100px;
            height: 36px;
            border: 0;
        }
        #add:hover{
            cursor: pointer;
        }
    </style>
    <script>
        $.ajaxSetup({
            async : false
        });
        $(function(){
            var userName=loginObj.user.userName;
            $("#userName").val(userName);
            var coachId=loginObj.user.filId;
            $("#coachId").val(coachId);
            //复选框赋值
            var url = "../../../NutritionAtsSchemeController/checkboxselectAll";
            var data = {coachId:coachId};
            $.post(url, data, function (obj) {
                if(obj){
                    $("#checkbbb").val("wuer");
                }
                $.each(obj, function (index, item) {
                    $("#checkBox1").append("<input type='checkbox' id='cc" + item.athleteId + "' name='athleteId' title='" + item.athleteName + "' value='" + item.athleteId + "'>");
                })
            }, "json");
            $("#btn").click(function () {
                $("#fileone").click();
                $("#fileone").change(function(obj){
                    var fileInput = $('#fileone').get(0).files[0];//获取input控件的file
                    if(fileInput){
                        $("#ts").html(fileInput.name);
                    }
                });

            });
            $("#btno").click(function () {
                $("#filetwo").click();
                $("#filetwo").change(function(obj){
                    var fileInput = $('#filetwo').get(0).files[0];//获取input控件的file
                    if(fileInput){
                        $("#tsone").html(fileInput.name);
                    }
                });
            });
            $("#btnt").click(function () {
                $("#filethree").click();
                $("#filethree").change(function(obj){
                    var fileInput = $('#filethree').get(0).files[0];//获取input控件的file
                    if(fileInput){
                        $("#tstwo").html(fileInput.name);
                    }
                });
            });
        })

    </script>
</head>
<body>
<form class="layui-form" action="" id="formDId" lay-filter="example" method="post" enctype="multipart/form-data">
    <input type="hidden" name="curriculumId" id="curriculumId">
    <input type="hidden" name="file1" id="file1">
    <input type="hidden" name="file2" id="file2">
    <input type="hidden" name="file3" id="file3">
    <input type="hidden" name="coachId" id="coachId">
    <input type="hidden" name="userName" id="userName">
    <input type="hidden" name="schemeId" id="schemeId">
    <input type="hidden" name="sportId" id="sportId">
    <input type="hidden" name="sportName" id="sportName">
    <div class="layui-form-item">
        <label class="layui-form-label">方案名称</label>
        <div class="layui-input-block" >
            <input type="text" name="schemeName" id="schemeName" required
                   lay-verify="required" placeholder="请输入方案名称" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label" style="color: red">附件1:*</label>
        <div class="layui-input-block">
            <input type="file" name="fileone" style="display: none" id="fileone"  autocomplete="off"
                   class="layui-input">
            <button id="btn"  type="button" >上传文件</button>
            <a id="ts" target="_blank" style="text-decoration:none"></a>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">附件2</label>
        <div class="layui-input-block">
            <input type="file" name="filetwo" style="display: none" id="filetwo"  autocomplete="off"
                   class="layui-input">
            <button id="btno"  type="button" >上传文件</button>
            <a id="tsone" target="_blank" style="text-decoration:none"></a>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">附件3</label>
        <div class="layui-input-block">
            <input type="file" name="filethree" style="display: none" id="filethree"  autocomplete="off"
                   class="layui-input">
            <button id="btnt"  type="button" >上传文件</button>
            <a id="tstwo" target="_blank" style="text-decoration:none"></a>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">运动员</label>
        <div id="checkBox1" class="layui-input-block">
            <input type="hidden" id="checkbbb">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn site-demo-active" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script>
    layui.use('laydate', function(){
        var laydate = layui.laydate;
        //时间范围
        laydate.render({
            elem: '#planDate'
            ,type: 'time'
        });
    });
    var form=null;
    layui.use('form', function(){
        form = layui.form;

        //监听提交
        form.on('submit(formDemo)', function(data){
            var cc=$("#checkbbb").val();
            if(cc== null ||cc== undefined || cc== ''){
                layer.msg('您还没有增加您的运动员，不能增加方案');
                return false;
            }

            var checkOne = false;                    //判断是否被选择条件
            var chboxVal = [];                       //存入被选中项的值
            var checkBox = $('input[name = athleteId]'); //获得得到所的复选框

            for (var i = 0; i < checkBox.length; i++) {

                //如果有1个被选中时（jquery1.6以上还可以用if(checkBox[i].prop('checked')) 去判断checkbox是否被选中）
                if (checkBox[i].checked) {
                    checkOne = true;
                    chboxVal.push(checkBox[i].value)//将被选择的值追加到
                };
            };

            if (!checkOne) {
                layer.msg("对不起：至少要选择一个运动员哦!");
                return false;
            }
            var fileInput = $('#fileone').get(0).files[0];//获取input控件的file
            var ide = getQueryString("id");
            if(ide== null || ide == undefined || ide == ''){
                if(fileInput== null || fileInput == undefined || fileInput == ''){
                    $("#ts").html('文件不能为空');
                    return false;
                }  else{
                    $("#ts").html('');
                }
            }

            var id = "";
            var title="";
            $("input[name='athleteId']:checked").each(function(index,items){
                id+=$(this).val()+",";
                title+=$(this).prop("title")+",";
            });
            $("#sportId").val(id);
            $("#sportName").val(title);
            var formData = new FormData(document.getElementById("formDId"));
            var url="../../../NutritionAtsSchemeController/addOrupdate";
            // 单击之后提交按钮不可选,防止重复提交
            var DISABLED = 'layui-btn-disabled';
            // 增加样式
            $('.site-demo-active').addClass(DISABLED);
            // 增加属性
            $('.site-demo-active').attr('disabled', 'disabled');
            $.ajax({
                url:url,
                type:"post",
                data:formData,
                processData:false,
                contentType:false,
                dataType:"json",
                success:function(obj){
                    if(obj.num==1){
                        layer.msg(obj.msg,{
                            icon:1,
                            time:1000 //1秒关闭(如果不配置默认三秒)
                        },function(){
                            //关闭当前层
                            //当你在iframe页面关闭自身时
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            //刷新父页面
                            //parent.location.reload();
                        });
                        return false ;
                    }
                },
                error:function(e){
                    layer.msg("错误！！");
                }
            });
            return false;
        });
        var ide = getQueryString("id");


        if(ide!=null){
            findById();//修改页面的赋值
            checboxfu();
            var str=$("#file1").val();
            var strone=$("#file2").val();
            var strtwo=$("#file3").val();
            if(str.length>1){
                $("#ts").text(str.substring(0,13)+"。。。。。 ."+str.match(/\.([^\.]+)$/)[1]);
                $("#ts").prop("href","../../../"+str);
            }
            if(strone.length>1){
                $("#tsone").html(strone.substring(0,13)+"。。。。。 ."+strone.match(/\.([^\.]+)$/)[1]);
                $("#tsone").prop("href","../../../"+strone);
            }
            if(strtwo.length>1){
                $("#tstwo").html(strtwo.substring(0,13)+"。。。。。 ."+strtwo.match(/\.([^\.]+)$/)[1]);
                $("#tstwo").prop("href","../../../"+strtwo);
            }
        }
    });


    //修改页面的赋值
    function findById(){
        var id=getQueryString("id");
        var url="../../../NutritionAtsSchemeController/findAll";
        var data={schemeId:id};
        $.post(url,data,function(obj){
            $("#schemeName").val(obj.schemeName);
            $("#file1").val(obj.file1);
            $("#file2").val(obj.file2);
            $("#file3").val(obj.file3);
            $("#schemeId").val(obj.schemeId);
        },"json");

    }

    /**
     * 复选框赋值
     */
    function checboxfu(){
        var id=getQueryString("id");
        var url="../../../NutritionAtsSchemeController/findByIdone";
        var data={schemeId:id};
        $.post(url,data,function(obj){
            $.each(obj,function(index,item){
                $("#cc"+item.athleteId).attr("checked","checked");
                form.render("checkbox");
            })
        },"json");
    }
</script>
</body>
</html>
      